package com.ruoyi.admin.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysFiles;
import com.ruoyi.system.service.ISysFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件管理Controller
 *
 * @author african-loan
 * @date 2021-04-20
 */
@RestController
@RequestMapping("/system/file")
public class SysFilesController extends BaseController {
    @Autowired
    private ISysFilesService sysFileService;

/**
 * 查询文件管理列表
 */
@PreAuthorize("@ss.hasPermi('system:file:list')")
@GetMapping("/list")
        public TableDataInfo list(SysFiles sysFile) {
        startPage();
        List<SysFiles> list = sysFileService.selectSysFilesList(sysFile);
        return getDataTable(list);
    }
    
    /**
     * 导出文件管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysFiles sysFile) {
        List<SysFiles> list = sysFileService.selectSysFilesList(sysFile);
        ExcelUtil<SysFiles> util = new ExcelUtil<SysFiles>(SysFiles. class);
        return util.exportExcel(list, "file");
    }

    /**
     * 获取文件管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysFileService.selectSysFilesById(id));
    }

    /**
     * 新增文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFiles sysFile) {
        return toAjax(sysFileService.insertSysFiles(sysFile));
    }

    /**
     * 修改文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFiles sysFile) {
        return toAjax(sysFileService.updateSysFiles(sysFile));
    }

    /**
     * 删除文件管理
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysFileService.deleteSysFilesByIds(ids));
    }
}
